const {
    controller,
    get,
    post,
    put
} = require('../lib/decorator');
const {
    getAllMovies,
    getMovieDetail,
    getRelativeMovies
} = require('../service/movie');

@controller('/api/v0/movies')
export class MovieController {
    // 获取所有电影
    @get('/')
    async getMovies(ctx, next) {
        const {
            type,
            year
        } = ctx.query;
        const movies = await getAllMovies(type, year);

        ctx.body = {
            code: 200,
            success: true,
            data: movies
        }
        await next();
    }

    // 根据id获取电影详情
    @get('/:id')
    async getDetail(ctx, next) {
        const id = ctx.params.id;
        const movie = await getMovieDetail(id);
        const relativeMovies = await getRelativeMovies(movie);

        ctx.body = {
            code: 200,
            success: true,
            data: {
                movie,
                relativeMovies
            }
        }
        await next();
    }
}
    // const Router = require('koa-router');
    // const router = new Router(); // 原始方法
    // router.get('/movies', async (ctx, next) => {
    //     const Movie = mongoose.model('Movie');
    //     const movies = await Movie.find({}).sort({
    //         'meta.createAt': -1 // 创建时间 从最近到最远排序
    //     })
    //     ctx.body = {
    //         movies
    //     }
    // })

    // router.get('/movies/:id', async (ctx, next) => {
    //     const Movie = mongoose.model('Movie');
    //     const id = ctx.params.id;
    //     const movie = await Movie.findOne({_id: id});
    //     ctx.body = {
    //         movie
    //     }
    // })

    // module.exports = router